#include<iostream>
#include"Bitmap.h"

/*************************************************************************************/

void loadBitmapFromFile(char *fileName, bitmapFile *bitmap)
{
	FILE *file;
	int bitmapSize;
	int i;
	
	file = fopen(fileName, "rb");
	if (!file)
		return;
	 
	fseek(file, 18, SEEK_SET);
	fread(&bitmap->width, 4, 1, file);

	fseek(file, 22, SEEK_SET);
	fread(&bitmap->height, 4, 1, file);

	fseek(file, 34, SEEK_SET);
	fread(&bitmapSize, 4, 1, file);

	bitmap->data = (unsigned char*)malloc(bitmapSize);
	fseek(file, 54, SEEK_SET);
	fread(bitmap->data, bitmapSize, 1, file);

	for (i = 0; i < bitmapSize; i += 3)
	{
		int tempByte = bitmap->data[i];
		bitmap->data[i] = bitmap->data[i+2];
		bitmap->data[i+2] = tempByte;
	}

	fclose(file);
}

/*************************************************************************************/

void freeBitmapFile(bitmapFile *bitmap)
{
	if (bitmap->data != NULL)
	{
		free(bitmap->data);
		bitmap->data = NULL;
	}
}

/*************************************************************************************/